package com.itheima.springboottest.service.impl;
import com.itheima.springboottest.mapper.UserMapper;
import com.itheima.springboottest.pojo.User;
import com.itheima.springboottest.service.UserServive;
import com.itheima.springboottest.utils.JwtUtil;
import com.itheima.springboottest.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserServive {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findById(Integer id) {
        System.out.println("执行到了这里");
        return userMapper.findById(id);
    }

    @Override
    public  void registerUser(String username,String password) {
        String md5String = Md5Util.getMD5String(password);
         userMapper.registerUser(username,md5String);
    }

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public String login(String username, String password) {
        String md5String = Md5Util.getMD5String(password);
        User user =  userMapper.findByUserName(username);
        System.out.println("user=="+user);
        System.out.println("md5String=="+ md5String);
        if(user.getUsername().equals(username)  && user.getPassword().equals(md5String)){


            Map<String,Object> result = new HashMap<>();
            result.put("id",user.getId());
            result.put("username",user.getUsername());
            //返回 token
            String token = JwtUtil.genToken(result);
            return token;
        }else {
            return "账号或者密码不正确";
        }

    }


}
